#include "CustomButtonWindow.h"
#include <QVBoxLayout>
#include <QHeaderView>
#include <QTimer>
#include <QMessageBox>
#include <QTableWidgetItem>
#include <QLabel>
#include "ElaTheme.h"
#include "ElaApplication.h"
#include "ElaAppBar.h"

CustomButtonWindow::CustomButtonWindow(QWidget* parent) : ElaWidget(parent)
{
    setWindowTitle("自定义按钮窗口示例");
    resize(600, 400);
    moveToCenter();

    // 自定义按钮配置 - 只显示特定按钮
    setWindowButtonFlags(ElaAppBarType::MinimizeButtonHint | ElaAppBarType::CloseButtonHint | ElaAppBarType::ThemeChangeButtonHint);

    // 或者逐个设置（注释掉上面的，使用下面的逐个设置）
    /*
    setWindowButtonFlag(ElaAppBarType::StayTopButtonHint, false);     // 禁用置顶
    setWindowButtonFlag(ElaAppBarType::MaximizeButtonHint, false);    // 禁用最大化
    setWindowButtonFlag(ElaAppBarType::NavigationButtonHint, false);  // 禁用导航
    setWindowButtonFlag(ElaAppBarType::MinimizeButtonHint, true);     // 启用最小化
    setWindowButtonFlag(ElaAppBarType::CloseButtonHint, true);        // 启用关闭
    setWindowButtonFlag(ElaAppBarType::ThemeChangeButtonHint, true);  // 启用主题切换
    */

    // 设置固定大小
    setIsFixedSize(true); // 不能拖拽设置窗口大小

    setupUI();

    //connect(this, &CustomButtonWindow::themeChangeButtonClicked, this, &CustomButtonWindow::onThemeChange);

    // 连接主题切换信号来更新显示
    connect(eTheme, &ElaTheme::themeModeChanged, this, &CustomButtonWindow::updateButtonInfo);

    // 更新按钮信息
    updateButtonInfo();
}

void CustomButtonWindow::setupUI()
{
    // 创建表格显示按钮状态
    m_buttonTable = new QTableWidget(this);
    m_buttonTable->setColumnCount(3);
    m_buttonTable->setHorizontalHeaderLabels({"按钮类型", "是否启用", "功能描述"});

    // 设置表格属性
    m_buttonTable->horizontalHeader()->setStretchLastSection(true);
    m_buttonTable->setEditTriggers(QAbstractItemView::NoEditTriggers);
    m_buttonTable->setSelectionBehavior(QAbstractItemView::SelectRows);
    m_buttonTable->setAlternatingRowColors(true);

    // 创建说明标签
    QLabel* infoLabel = new QLabel(
        "此窗口演示了如何自定义标题栏按钮。\n"
        "当前只启用了最小化、关闭和主题切换按钮。",
        this);
    infoLabel->setWordWrap(true);
    infoLabel->setStyleSheet("padding: 10px; background: rgba(0,0,0,0.05); border-radius: 5px;");

    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->setContentsMargins(20, getAppBarHeight() + 20, 20, 20);
    layout->setSpacing(15);
    layout->addWidget(infoLabel);
    layout->addWidget(m_buttonTable);

    setLayout(layout);
}

void CustomButtonWindow::updateButtonInfo()
{
    // 获取当前按钮状态
    ElaAppBarType::ButtonFlags flags = getWindowButtonFlags();

    // 定义按钮信息
    QVector<QPair<QString, ElaAppBarType::ButtonType>> buttons = {{"置顶按钮", ElaAppBarType::StayTopButtonHint},
                                                                  {"最小化按钮", ElaAppBarType::MinimizeButtonHint},
                                                                  {"最大化按钮", ElaAppBarType::MaximizeButtonHint},
                                                                  {"关闭按钮", ElaAppBarType::CloseButtonHint},
                                                                  {"主题切换按钮", ElaAppBarType::ThemeChangeButtonHint},
                                                                  {"导航按钮", ElaAppBarType::NavigationButtonHint}};

    QVector<QString> descriptions = {"将窗口保持在其他窗口之上", "最小化窗口到任务栏", "最大化窗口或恢复原大小", "关闭窗口", "切换浅色/深色主题", "显示导航菜单"};

    // 更新表格
    m_buttonTable->setRowCount(buttons.size());

    for (int i = 0; i < buttons.size(); ++i)
    {
        bool isEnabled = flags & buttons[i].second;

        // 按钮类型
        QTableWidgetItem* typeItem = new QTableWidgetItem(buttons[i].first);
        m_buttonTable->setItem(i, 0, typeItem);

        // 启用状态
        QTableWidgetItem* statusItem = new QTableWidgetItem(isEnabled ? "✅ 启用" : "❌ 禁用");
        statusItem->setTextAlignment(Qt::AlignCenter);
        m_buttonTable->setItem(i, 1, statusItem);

        // 功能描述
        QTableWidgetItem* descItem = new QTableWidgetItem(descriptions[i]);
        m_buttonTable->setItem(i, 2, descItem);

        // 根据状态设置颜色
        if (isEnabled)
        {
            statusItem->setBackground(QColor(240, 255, 240));
        }
        else
        {
            statusItem->setBackground(QColor(255, 240, 240));
        }
    }

    m_buttonTable->resizeColumnsToContents();
}

//void CustomButtonWindow::onThemeChange()
//{
//    ElaThemeType::ThemeMode currentMode = eTheme->getThemeMode();
//    ElaThemeType::ThemeMode newMode = (currentMode == ElaThemeType::Light) ? ElaThemeType::Dark : ElaThemeType::Light;
//    eTheme->setThemeMode(newMode);
//}
